﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Account_RoleModify : BasePage
{
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        base.CheckUserPermission("001003");

        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            //加载模块列表
            BindRepeateData();

            if (Action == "Update")
            {
                //加载基本数据
                getDate(Id);

                //加载模块列表数据
                GetRoleMenu(Id);
            }
        }
    }
    #endregion

    #region 加载数据
    private void getDate(int id)
    {
        sys_role model = new sys_role();
        sys_roleBLL bll = new sys_roleBLL();
        model = bll.GetModel(id);
        this.txt_RoleName.Text = model.RoleName;
        this.radio_Valid.SelectedValue = model.Valid.ToString();
    }

    private void BindRepeateData()
    {
        List<sys_menu> list = new List<sys_menu>();
        sys_menuBLL bll = new sys_menuBLL();
        list = bll.GetModelList("Valid=1");
        this.RepeaterList.DataSource = list;
        this.RepeaterList.DataBind();
    }

    private void GetRoleMenu(int id)
    {
        List<sys_rolemenu> list = new List<sys_rolemenu>();
        sys_rolemenuBLL bll = new sys_rolemenuBLL();
        list = bll.GetModelList("RoleId=" + id);

        //插入新数据
        for (int i = 0; i < RepeaterList.Items.Count; i++)
        {
            CheckBox check_Menu = (CheckBox)RepeaterList.Items[i].FindControl("check_Menu");
            HiddenField hd_MenuId = (HiddenField)RepeaterList.Items[i].FindControl("hd_MenuId");

            int menuid = Convert.ToInt32(hd_MenuId.Value);

            if (list.FindAll(delegate(sys_rolemenu p) { return p.MenuId == menuid; }).Count > 0)
            {
                check_Menu.Checked = true;
            }
            else
            {
                check_Menu.Checked = false;
            }
        }
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (Action == "Update")
        {
            #region 修改
            try
            {
                sys_role model = new sys_role();
                sys_roleBLL bll = new sys_roleBLL();
                sys_rolemenuBLL rolemenubll = new sys_rolemenuBLL();

                //检验数据是否重复
                string strCheck = string.Format("RoleName like '{0}' and RoleId<>{1}",
                    TextUtility.ReplaceText(this.txt_RoleName.Text.Trim()),
                    Id.ToString());
                if (bll.GetModelList(strCheck).Count > 0)
                {
                    base.Alert("角色名称已存在！");
                    return;
                }

                model = bll.GetModel(Id);
                model.RoleName = this.txt_RoleName.Text.Trim();
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.UpdateUser = base.CurrentSession.UserID;
                model.UpdateDate = DateTime.Now;

                bool isSuccess = bll.Update(model);

                if (isSuccess)
                {
                    int roleid = model.RoleId;
                    //删除旧数据
                    rolemenubll.Delete("RoleId=" + roleid);

                    //插入新数据
                    for (int i = 0; i < RepeaterList.Items.Count; i++)
                    {
                        CheckBox check_Menu = (CheckBox)RepeaterList.Items[i].FindControl("check_Menu");
                        HiddenField hd_MenuId = (HiddenField)RepeaterList.Items[i].FindControl("hd_MenuId");

                        if (check_Menu.Checked)
                        {
                            int menuid = Convert.ToInt32(hd_MenuId.Value);
                            sys_rolemenu rolemenu = new sys_rolemenu();
                            rolemenu.MenuId = menuid;
                            rolemenu.RoleId = roleid;

                            rolemenubll.Add(rolemenu);
                        }
                    }
                }

                base.Redirect("操作成功！", "RoleList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }
        else
        {
            #region 新增
            try
            {
                sys_role model = new sys_role();
                sys_roleBLL bll = new sys_roleBLL();
                sys_rolemenuBLL rolemenubll = new sys_rolemenuBLL();
                //检验数据是否重复
                string strCheck = string.Format("RoleName like '{0}'",
                    TextUtility.ReplaceText(this.txt_RoleName.Text.Trim()));
                if (bll.GetModelList(strCheck).Count > 0)
                {
                    base.Alert("模块名称或模块编码已存在！");
                    return;
                }

                model.RoleName = this.txt_RoleName.Text.Trim();
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;
                bool isSuccess = bll.Add(model);

                if (isSuccess)
                {
                    int newid = new utilityBLL().GetNewId();
                    //插入模块数据
                    for (int i = 0; i < RepeaterList.Items.Count; i++)
                    {
                        CheckBox check_Menu = (CheckBox)RepeaterList.Items[i].FindControl("check_Menu");
                        HiddenField hd_MenuId = (HiddenField)RepeaterList.Items[i].FindControl("hd_MenuId");

                        if (check_Menu.Checked)
                        {
                            int menuid = Convert.ToInt32(hd_MenuId.Value);
                            sys_rolemenu rolemenu = new sys_rolemenu();
                            rolemenu.MenuId = menuid;
                            rolemenu.RoleId = newid;

                            rolemenubll.Add(rolemenu);
                        }
                    }
                }

                base.Redirect("操作成功！", "RoleList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }

    }
    #endregion
}
